package dfs;

import java.util.ArrayList;
import java.util.List;

/**
 * 给定一个字符串S，通过将字符串S中的每个字母转变大小写，我们可以获得一个新的字符串。返回所有可能得到的字符串集合。
 * https://leetcode-cn.com/problems/letter-case-permutation/
 *
 * @author: luo
 * @email: luo@nyist.edu.cn
 * @date: 2021/1/21 12:51
 */
public class LetterCasePermutation {
    public static void main(String[] args) {
        List<String> list = letterCasePermutation("3z4");
        for (String str : list) {
            System.out.println(str);
        }
    }

    public static List<String> letterCasePermutation(String s) {
        char[] chars = s.toCharArray();
        char[] switchChars = new char[chars.length];
        List<String> list = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
           if (chars[i] >= 'a'&&chars[i]<='z') {
                switchChars[i] = (char) (chars[i] - 32);
           } else if (chars[i] >= 'A' && chars[i] <= 'Z') {
               switchChars[i] = (char) (chars[i] + 32);
           } else {
               switchChars[i] = chars[i];
           }
        }
        backtracking(0, new char[chars.length], chars, switchChars, list);
        return list;
    }

    private static void backtracking(int n, char[] chs, char[] chars, char[] switchChars, List<String> list) {
        if (n >= chars.length) {
            list.add(new String(chs));
        } else {
            if (chars[n] == switchChars[n]) {
                chs[n] = chars[n];
                backtracking(++n, chs, chars, switchChars, list);
            } else {
                chs[n] = chars[n];
                backtracking(++n, chs, chars, switchChars, list);

                chs[n-1] = switchChars[n - 1];
                backtracking(n, chs, chars, switchChars, list);
            }

        }

    }
}
